package parser;

import data.Event;
import data.Office;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author David Csejtei, CodeGuide Team, http://codeguide.hu
 */
public class txtParser {
   
    // Non-instantiate class
    private txtParser() {
    }
    
    // Get the directory of input files
    public static final String inputDir = System.getProperty("user.dir")+"\\inputfiles\\";
    
    // File for the details of the events 
    public static final String eventTxt = "sportesemenyek.txt";
    public static final String officeTxt = "fogadoirodak.txt";
    
    // fogadoirodak.txt - irodák
    // sportfogadok.txt - emberek
    
    public static List<Event> readEventsFromTxt() {
        // For the return
        List<Event> events = new ArrayList<Event>();
        
        // Number of the event attributes
        int attrCount = 6;
        
        // For the file reading
        FileInputStream input = null;
        BufferedReader reader = null;
        String line = null;

        try {
            // Set the readers
            input = new FileInputStream(inputDir+eventTxt);
            reader = new BufferedReader(new InputStreamReader(input));

            // The event
            Event event = null;
            
            // Help to identify the lines
            int count = 1;
            
            // Read the lines
            while((line = reader.readLine()) != null){
                // The start times line
                if(Double.compare(1.0,(count % attrCount)) == 0) {
                    // Create a new event
                    event = new Event();
                    event.setStartTime(Integer.parseInt(line));
                    //System.out.println(count);
                }
                // Set the end time
                if(Double.compare(2.0,(count % attrCount)) == 0) {
                    if(event != null) 
                         event.setEndTime(Integer.parseInt(line));
                    //System.out.println(count);
                }
                // Set the title
                if(Double.compare(3.0,(count % attrCount)) == 0) {
                    if(event != null) 
                         event.setName(line);
                    //System.out.println(count);
                }
                // The 1 chance line
                if(Double.compare(4.0,(count % attrCount)) == 0) {
                    if(event != null) 
                        event.setOneChance(Float.parseFloat(line));
                     //System.out.println(count);
                }
                // The 2 chance line
                if(Double.compare(5.0,(count % attrCount)) == 0) {
                    if(event != null) 
                        event.setTwoChance(Float.parseFloat(line));
                    //System.out.println(count);
                }
                // The x chance line
                if(Double.compare(0.0,(count % attrCount)) == 0) {
                    if(event != null) {
                        event.setxChance(Float.parseFloat(line));
                        events.add(event);
                        //System.out.println("A new event added to the event list.");
                    }
                }
                
                count++;
            }
        } catch (IOException | NumberFormatException e) {
            System.out.println("Exception:" + e.getMessage() + " : " + e.getClass());
        }
        finally {
            // Try to close the readers
            try {
                if(reader != null)
                    reader.close();
                if(input != null)
                    input.close();
            } catch(IOException ie) {
                System.out.println("Exception:" + ie.getMessage() + " : " + ie.getClass());
            }
        }
        
        return events;
    }
    
    public static List<Office> readOfficesFromTxt() {
        // For the return
        List<Office> offices = new ArrayList<Office>();
        
        // For the file reading
        FileInputStream input = null;
        BufferedReader reader = null;
        String line = null;

        try {
            // Set the readers
            input = new FileInputStream(inputDir+officeTxt);
            reader = new BufferedReader(new InputStreamReader(input));

            // The event
            Office office = null;
            
            // Help to identify the lines
            int count = 1;
            
            // Read the lines
            while((line = reader.readLine()) != null){
                // Create new office and set it's name
                if(Double.compare(1.0,(count % 4)) == 0) {
                    office = new Office(line);
                }
                // Set the port number
                if(Double.compare(2.0,(count % 4)) == 0) {
                    office.setPortNumber(Integer.parseInt(line));
                }
                // Set the maximum amount of bet
                if(Double.compare(3.0,(count % 4)) == 0) {
                    office.setMaxAmount(Integer.parseInt(line));
                }
                // Set the number of p
                if(Double.compare(0.0,(count % 4)) == 0) {
                    office.setP(Integer.parseInt(line));
                    offices.add(office);
                    //System.out.println("A new office added to the office list.");
                }
                
                count++;
            }
        } catch (IOException | NumberFormatException e) {
            System.out.println("Exception:" + e.getMessage() + " : " + e.getClass());
        }
        finally {
            // Try to close the readers
            try {
                if(reader != null)
                    reader.close();
                if(input != null)
                    input.close();
            } catch(IOException ie) {
                System.out.println("Exception:" + ie.getMessage() + " : " + ie.getClass());
            }
        }
        
        return offices;
    }

}
